package cn.wx.read.es.service;

import java.io.IOException;
import java.util.List;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wx.common.unitofwork.aop.UseUnitOfWork;
import cn.wx.read.es.entity.EsExpert;
import cn.wx.read.es.repository.EsExpertRepository;


@Service
@Transactional
public class EsExpertService {
	
	private final static String INDEX = "es_expert";
	private final static String TYPE = "esexpert";
	
	
	
	@Autowired
	EsExpertRepository esExpertRepository;
	
	@Autowired
	private Client client;
	
	public void importExpert(EsExpert ne) {
		esExpertRepository.save(ne);
	}
	
	/**
	 * 更新 hindex 、gindex
	 */
	@UseUnitOfWork(saga = "EsHGIndex")
	public void importHGIndex(List<EsExpert> nes) {
		
		if(nes == null || nes.size() == 0){
			return;
		}
		
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		UpdateRequest updateRequest = null;
		for(EsExpert ne : nes) {
			
			try {
				String pId = ne.getId();//TypeConversion.converLong_String(ne.getId());
				XContentBuilder c = XContentFactory.jsonBuilder().startObject();
				c.field("hIndex",ne.gethIndex());
				c.field("gIndex",ne.getgIndex());
				c.endObject();
				updateRequest = new UpdateRequest(INDEX, TYPE, pId).doc(c);
				bulkRequest.add(updateRequest);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		bulkRequest.execute().actionGet();
	}
	
	
	/**
	 * 更新 专利patents
	 */
	@UseUnitOfWork(saga = "EsExpertPatents")
	public void importExpertPatents(List<EsExpert> nes) {
		
		if(nes == null || nes.size() == 0){
			return;
		}
		
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		UpdateRequest updateRequest = null;
		for(EsExpert ne : nes) {
			
			try {
				String pId = ne.getId();//TypeConversion.converLong_String(ne.getId());
				XContentBuilder c = XContentFactory.jsonBuilder().startObject();
				c.field("patentsNum",ne.getPatentsNum());
				c.field("patentsCit",ne.getPatentsCit());
				c.field("efpData",ne.getEfpData());
				c.field("eqWorkData",ne.getEqWorkData());
				c.field("eqWork4Data",ne.getEqWork4Data());
				c.endObject();
				updateRequest = new UpdateRequest(INDEX, TYPE, pId).doc(c);
				bulkRequest.add(updateRequest);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		bulkRequest.execute().actionGet();
	}
	
	/**
	 * 导入专家新闻社交统计信息
	 */
	@UseUnitOfWork(saga = "EsExpertNews")
	public void importExpertNews(List<EsExpert> nes) {
		
		if(nes == null || nes.size() == 0){
			return;
		}
		
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		UpdateRequest updateRequest = null;
		for(EsExpert ne : nes) {
			
			try {
				String pId = ne.getId();//TypeConversion.converLong_String(ne.getId());
				XContentBuilder c = XContentFactory.jsonBuilder().startObject();
				c.field("nnum",ne.getNnum());
				c.field("snum",ne.getSnum());
				c.endObject();
				updateRequest = new UpdateRequest(INDEX, TYPE, pId).doc(c);
				bulkRequest.add(updateRequest);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		bulkRequest.execute().actionGet();
	}
	
	/**
	 * 批量插入专家
	 * @param nes
	 */
	@UseUnitOfWork(saga = "EsBetchExpert")
	public void importBetchExpert(List<EsExpert> nes){
		if(nes == null || nes.size() == 0){
			return;
		}
		esExpertRepository.saveAll(nes);
	}
	
}
